package com.itheima.controller;

import com.itheima.anno.LogAnno;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import com.itheima.vo.PageBean;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping("/emps")
    @LogAnno(methodDesc="分页查询员工数据")
    public Result findByPage(
            String name, Integer gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize

    ){
        PageBean<Emp> pageBean=empService.finByPage(page,pageSize,name,gender,begin,end);
        return  Result.success(pageBean);
    }

    @PostMapping("/emps")
    @LogAnno(methodDesc="添加员工数据")
    public  Result save(@RequestBody Emp emp){
        empService.save(emp);
        return  Result.success();
    }
   @GetMapping("/emps/{id}")
   @LogAnno(methodDesc="分页查询员工数据")
    public  Result selectById(@PathVariable("id") Integer id){
        Emp emp = empService.selectById(id);
        return Result.success(emp);
   }
   @PutMapping("/emps")
   @LogAnno(methodDesc="修改员工数据")
    public Result update(@RequestBody Emp emp){
        empService.update(emp);
        return  Result.success();
   }
   @DeleteMapping("/emps/{ids}")
   @LogAnno(methodDesc="删除员工数据")
    public Result delete(@PathVariable List<Integer> ids){
        empService.delete(ids);
        return Result.success();
   }
}
